package com.gtt.lazy.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.type.Type;

import com.gtt.lazy.domain.Category;
import com.gtt.lazy.domain.CategoryToSofa;
import com.gtt.lazy.domain.Sofa;
import com.gtt.lazy.service.CategorySofaService;
import com.gtt.lazy.util.HibernateTemplateUtil;

public class CategorySofaServiceImpl implements CategorySofaService {

	private static final Logger logger = Logger
			.getLogger(SofaServiceImpl.class);

	private HibernateTemplateUtil hibernateTemplateUtil;

	public int add(CategoryToSofa cts) {
		if (logger.isDebugEnabled())
			logger.debug("add CategoryToSofa ");
		return (Integer) hibernateTemplateUtil.save(cts);
	}

	@SuppressWarnings("unchecked")
	public boolean checkExist(CategoryToSofa cts) {
		String hql = "from CategoryToSofa as cts where cts.cid=? and cts.sid=?";
		List list = hibernateTemplateUtil.find(hql, new Object[] {
				cts.getCid(), cts.getSid() }, new Type[] { Hibernate.INTEGER,
				Hibernate.INTEGER });
		return list.size() > 0;
	}

	public void delete(int id) {
		if (logger.isDebugEnabled())
			logger.debug("delete CategoryToSofa [ " + id + " ]");
		hibernateTemplateUtil.delete(CategoryToSofa.class, id);
	}

	public void delete(CategoryToSofa cts) {
		if (logger.isDebugEnabled())
			logger.debug("delete CategoryToSofa [ " + cts.getCid() + ":"
					+ cts.getSid() + " ]");
		String hql = "delete from CategoryToSofa as cts where cts.cid=? and cts.sid=?";
		hibernateTemplateUtil.update(hql, new Object[] { cts.getCid(),
				cts.getSid() }, new Type[] { Hibernate.INTEGER,
				Hibernate.INTEGER });
	}

	@SuppressWarnings("unchecked")
	public List<Category> listCategories(int sid) {
		String sql = "select c.* from tb_category as c"
				+ " inner join tb_cat_sofa as cts on c.id=cts.cid"
				+ " where cts.sid=" + sid + " order by c.id desc";
		return hibernateTemplateUtil.findBySql(sql, "c", Category.class);
	}

	@SuppressWarnings("unchecked")
	public List<Category> listCategories(int sid, int start, int size) {
		String sql = "select c.* from tb_category as c"
				+ " inner join tb_cat_sofa as cts on c.id=cts.cid"
				+ " where cts.sid=" + sid + " order by c.id desc";
		return hibernateTemplateUtil.findBySql(sql, "c", Category.class, start,
				size);
	}

	@SuppressWarnings("unchecked")
	public List<Sofa> listSofas(int cid) {
		String sql = "select s.* from tb_sofa as s"
				+ " inner join tb_cat_sofa as cts on s.id=cts.sid"
				+ " where cts.cid=" + cid + " order by s.publish_date desc";
		return hibernateTemplateUtil.findBySql(sql, "s", Sofa.class);
	}

	@SuppressWarnings("unchecked")
	public List<Sofa> listSofas(int cid, int start, int size) {
		String sql = "select s.* from tb_sofa as s"
				+ " inner join tb_cat_sofa as cts on s.id=cts.sid"
				+ " where cts.cid=" + cid + " order by s.publish_date desc";
		return hibernateTemplateUtil.findBySql(sql, "s", Sofa.class, start,
				size);
	}

	public void setHibernateTemplateUtil(
			HibernateTemplateUtil hibernateTemplateUtil) {
		this.hibernateTemplateUtil = hibernateTemplateUtil;
	}

	public int countCategories(int sid) {
		String sql = "select count(*) from tb_cat_sofa as cts where cts.sid="
				+ sid;
		return ((Number) hibernateTemplateUtil.findBySql(sql).get(0))
				.intValue();
	}

	public int countSofas(int cid) {
		String sql = "select count(*) from tb_cat_sofa as cts where cts.cid="
				+ cid;
		return ((Number) hibernateTemplateUtil.findBySql(sql).get(0))
				.intValue();
	}

}
